//
//  BBBaseViewController.swift
//  BBetter
//
//  Created by Gmc on 2020/3/1.
//  Copyright © 2020 Gmc. All rights reserved.
//

import UIKit
import SnapKit

class BBBaseViewController: UIViewController {
    
    open var loadCall:(()->Void)?
    open var tableView = UITableView()
        override func viewDidLoad() {
            super.viewDidLoad()
            initNavButton()
            initTable()
//            navigationController?.navigationBar.isTranslucent = false
        }
    
    private func initNavButton() {
        let button = UIButton(type: .custom)
        button.setTitle("添加", for: .normal)
        if (CheckDate.check(date: "04-04") || CheckDate.check(date: "12-13")) {
            button.setTitleColor(UIColor.colorWithHexString(hexString: "272727"), for: .normal)
        } else {
            button.setTitleColor(UIColor.colorWithHexString(hexString: "00BFFF"), for: .normal)
        }

        button.setTitleColor(UIColor.colorWithHexString(hexString: "DDDDDF"), for: .highlighted)
        button.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
        let rightButton = UIBarButtonItem(customView: button)
        self.navigationItem.rightBarButtonItem = rightButton
    }
    
    private func initTable() {
        view.addSubview(tableView)
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.showsVerticalScrollIndicator = false
        tableView.register(BBDisplayCell.self, forCellReuseIdentifier: "cellID")
        tableView.snp.makeConstraints { (make) in
            make.leading.trailing.equalTo(view)
            make.top.equalTo(view).offset(0)
            make.bottom.equalTo(view).offset(0)
        }
    }
    
    @objc open func buttonAction(sender:UIBarButtonItem) {
        
    }
    
    func sendMessageToParent(obj:Any) {
        if let p = parent {
            if p.children.count > 1 {
                let selfIndex = p.children.firstIndex(of: self) ?? 0
                let parent:BBBaseViewController = p.children[selfIndex - 1] as! BBBaseViewController
                parent.sendMessageToParent(obj: obj)
            }
        }
    }

}

extension BBBaseViewController: UITableViewDelegate, UITableViewDataSource {
        
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 10
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath)
            return cell
        }
        
        func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
            
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 80.0
        }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        if offsetY < -200 {
            self.loadCall?()
        }
    }
}
